import os
from utils import doc_util, json_util, read_config
from database.mysqlhelper import MySqLHelper

db = MySqLHelper()
config_path = os.path.join(read_config.data_path, 'common', 'config')

insert_sql = 'insert into t_stock_info (f_user_code,f_user_name,f_prompt_code) VALUES (%s, %s, %s) '
select_sql = 'select f_user_code,f_user_name,f_prompt_code,f_update_time from t_stock_info '


# 初始化所有股票
def insert(u_info):
    li = ((u_info['user_code'], u_info['user_name'], u_info['prompt_code']))
    return db.insertone(insert_sql, li)


# 获取一条
def selectone(code):
    # 增加多条
    sql = select_sql + ' where f_user_code = %s '
    return format_one(db.selectone(sql, (code)))


# 获取所有
def selectall():
    return format_res(db.selectall(select_sql))


# 格式化结果
def format_res(res):
    list = []
    for p in res:
        list.append(format_one(p))
    return list


# 格式化结果
def format_one(res):
    if not res:
        return {}
    return {'user_code': str(res[0], encoding="utf-8"), 'user_name': str(res[1], encoding="utf-8"),
            'prompt_code': str(res[2], encoding="utf-8"), 'up_time': res[3]}
